﻿var reType = "Windows";
//登录方法
function Login() {
    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(login)', function (data) {
            $.ajax({
                url: "../Basis/funLogin",
                type: "post",
                data: JSON.stringify({
                    "userCode": data.field["username"], "userPwd": data.field["password"], "reType": reType
                }),
                success: function (datas) {
                    var dataObj = eval("(" + datas + ")");//转换为json对象 
                     if (dataObj.data=="用户不存在") {
                        layer.msg(dataObj.data);
                    } else if (dataObj.data =="密码错误") {
                        layer.msg(dataObj.data);
                     } else {
                          location.href = "Index";
                      }
                }
            });
            return false;
        });
    });
}

//弹出层
function OpenPage(title, pageUrl) {
    if (title == undefined || title == "") {
        title = "新建窗口";
    }
    var w = window.screen.width;
    var h = window.screen.height;
    var url = pageUrl;
    layer.open({
        type: 2,
        title: title,
        skin: 'layui-layer-rim', //加上边框
        area: ['95%', '90%'],
        content: url
    });
}

//用户列表
function getUserList() {
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function () {
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
            , carousel = layui.carousel //轮播
            , upload = layui.upload //上传
            , element = layui.element //元素操作
            , slider = layui.slider //滑块


        //给table赋值
        table.render({
            elem: '#userList'
            , height: 380
            //, width: 1030
            , url: '../Basis/getUserList' //数据接口
            , title: '用户表'
            , page: true //开启分页
            , even: true
            , limits:  [20, 50, 80,100]
            , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,defaultToolbar: ['print', 'exports']
            , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }
                , { field: 'userCode', title: '用户账号', width: 80, sort: true, fixed: 'left' }
                , { field: 'userName', title: '用户名称', width: 80 }
                , { field: 'userDep', title: '用户部门', width: 90 }
                , { field: 'userPhone', title: '联系方式', width: 110, sort: true }
                , { field: 'createTime', title: '创建时间', width: 180 }
                , { field: 'createUser', title: '创建人员', width: 150 }
                , { field: 'accCode', title: '所属账套', width: 100 }
                , { field: 'isAccName', title: '同步数据', width: 100 }
                , { fixed: 'right',title:'操作', width: 80, align: 'center', toolbar: '#barDemo' }
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    OpenPage('添加用户', '../Basis/EditUser?type=Add');
                    getUserList();
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        OpenPage('修改用户', '../Basis/EditUser?type=' + checkStatus.data[0].userCode + '');
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //eg1       
                        layer.confirm('确认删除吗？', {
                            btn: ['是', '否'] //可以无限个按钮
                            , btn2: function (index, layero) {
                                //否
                            }
                        }, function (index) {
                            //是
                            var CodeList = "";
                            for (var i = 0; i < data.length; i++) {
                                if (CodeList == "") {
                                    CodeList = data[i].userCode;
                                } else {
                                    CodeList += "," + data[i].userCode;
                                }
                            }
                            $.ajax({
                                url: "../Basis/DelUser",
                                type: "post",
                                data: CodeList,
                                success: function (data) {
                                    if (data == 1) {
                                        layer.msg("删除成功");
                                    } else {
                                        layer.msg("删除失败");
                                    }
                                    getUserList();
                                }
                            });
                            layer.close(index);
                        });
                    }
                    break;
            }
        });


         //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'reSetPwd') {  
                layer.confirm('确认重置密码？', {
                    btn: ['是', '否'] //可以无限个按钮
                    , btn2: function (index, layero) {
                        //否
                    }
                }, function (index) {
                    layer.close(index);
                    //是
                    $.ajax({
                        url: "../Basis/reSetPwd",
                        data: JSON.stringify({ "userCode": data.userCode }),
                        type: "post",
                        success: function (data) {
                            if (data == 1) {
                                layer.msg("成功");
                            } else {
                                layer.msg(data);
                            }
                        }
                    });
                });
            } 
        });
    });
}
 
//关闭当前弹出层
function ClosePage() {
    getUserList();
    //获取当前弹出层id
    var index = parent.layer.getFrameIndex(window.name);
    ////关闭当前frame
    parent.layer.close(index);
}

//维护用户页面点击提交方法
function AddEdit(datas) {
    var type = getCondition();
    var Pwd = datas.userPwd;
    var rePwd = datas.rePwd;
    if (Pwd != rePwd) {
        layer.msg("两次输入密码不一致");
    } else {
        //添加
        $.ajax({
            url: "../Basis/AddUser",
            type: "post",
            data: JSON.stringify({ "data": datas, "type": type }),
            success: function (data) {
                if (data ==1) {
                    ClosePage();
                } else {
                    layer.msg(data);
                }
            }
        });
    }
}

//获取页面跳转时传输的值
function getCondition() {
    var type = location.search;
    type = type.substring(6,type.length);
    return type;
}

//维护用户页面加载
function EditUserLoad() {
    var type = getCondition();
    if (type!="Add") {
        $("#userCode").attr("readonly", "readonly");
        $("#userCode").val(type);
        $.ajax({
            url: "../Basis/getUserList",
            type: "post",
            data: JSON.stringify({"userCode":type}),
            success: function (data) {
                var json = JSON.parse(data);
                json = json.data[0];
                $("#userName").val(json.userName);
                $("#userDep").val(json.userDep);
                $("#userPhone").val(json.userPhone);
                $("#userPwd").val(json.userPwd);
                $("#rePwd").val(json.userPwd);
                if (json.isAcc==1) {
                    $("#userName").attr("readonly","readonly");
                    $("#userDep").attr("readonly", "readonly");
                    $("#userPhone").attr("readonly", "readonly");
                }
            }
        })
    }
}

//维护用户页面重置
function reset() {
    var type = getCondition();
    $("#userName").val("");
    $("#userDep").val("");
    $("#userPwd").val("");
    $("#rePwd").val("");
    $("#userPhone").val("");
    if (type == "Add") {
        $("#userCode").val("");
    }
}

//同步用户信息
function synchronous() {
    $.ajax({
        url: "../Basis/synchronous",
        type: "post",
        success: function (data) {
            getUserList();
        }
    })
}